<?php

namespace App\Http\Controllers;

use App\Http\Requests\RoleRequest;
use App\Models\Role;
use App\Services\CommonService;
use App\Services\RoleService;
use Illuminate\Http\Request;

/**
 * 角色控制器
 * Class RoleController
 * @package App\Http\Controllers
 */
class RoleController extends Controller
{
    public $roleService;

    public function __construct(RoleService $roleService)
    {
        parent::__construct();
        $this->roleService = $roleService;
    }

    /**
     * 角色列表
     */
    public function index(Request $request)
    {
        $perPage = $request->input('per_page') ?: $this->limit;

        $roles = $this->roleService->getList($request->all(), $perPage);

        return $this->success($roles);
    }

    /**
     * 获取指定 id 的数据
     */
    public function show(Request $request)
    {
        $id   = $request->input('id');
        $data = $this->roleService->getInfo($id);

        return $this->success($data);
    }

    /**
     * 添加一个角色
     */
    public function store(Request $request)
    {
        $params = $request->all();
        if (in_array($params['title'], config('site.forbid_admin_name'))) {
            return $this->error('禁止使用该名称！');
        }
        if (!$this->roleService->saveData($params)){
            return $this->error('操作失败！');
        }

        return $this->success('', '操作成功！');
    }

    /**
     * 回收站列表（进行软删除的数据）
     */
    public function recycle(Request $request)
    {
        $data = $this->roleService->getRecycles($request);

        return $this->success($data);
    }

    /**
     * 批量软删除，ids可以是以","逗号分割的字符串，也可以是数组形式
     */
    public function destroyMore(Request $request)
    {
        $ids = to_array($request->input('ids'));
        if (!Role::destroy($ids)) {
            return $this->error('删除失败！');
        };
        $ids = implode(',', $ids);
        CommonService::addActionLog("删除角色", "【数据ID={$ids}】");

        return $this->success('', '放入回收站成功');
    }

    /**
     * 恢复批量数据
     */
    function retrieveMore(Request $request)
    {
        $ids = to_array($request->input('ids'));
        if (!Role::query()->whereIn('id', $ids)->restore()) {
            return $this->error('恢复失败！');
        }
        $ids = implode(',', $ids);
        CommonService::addActionLog("恢复角色", "【数据ID={$ids}】");

        return $this->success('', '恢复成功');
    }

    /**
     * 永久删除一条数据
     */
    public function delete(Request $request)
    {
        $id = $request->input('id', 0);
        if (!Role::withTrashed()->find($id)->forceDelete()) {
            return $this->error('删除失败！');
        }
        CommonService::addActionLog("删除角色", "【数据ID={$id}】");

        return $this->success('', '删除成功');
    }

    /**
     * 永久删除批量数据，ids可以是以","逗号分割的字符串，也可以是数组形式
     */
    public function deleteMore(Request $request)
    {
        $ids = to_array($request->input('ids'));
        if (!Role::withTrashed()->whereIn('id', $ids)->forceDelete()) {
            return $this->error('删除失败！');
        }
        $ids = implode(',', $ids);
        CommonService::addActionLog("删除角色", "【数据ID={$ids}】");

        return $this->success('', '删除成功');
    }


}
